#!/bin/sh

. "$SPEC_PATH/abstract/dbops-minor-version-upgrade"

e2e_test_extra_hash() {
  "$SHELL" "$PROJECT_PATH/stackgres-k8s/ci/build/build-functions.sh" path_hash \
    "$(realpath --relative-to "$PROJECT_PATH" "$SPEC_PATH/abstract/dbops-minor-version-upgrade")"
}

e2e_test_install() {
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 1 \
    --set-string "cluster.postgres.version=$E2E_MINOR_SOURCE_POSTGRES_VERSION" \
    --set-string "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE"

  deploy_curl_pod "$CLUSTER_NAMESPACE"

  wait_pods_running "$CLUSTER_NAMESPACE" 1

  DBOPS_NAME=minor-version-upgrade

  cat << EOF | kubectl create -f -
apiVersion: stackgres.io/v1
kind: SGDbOps
metadata:
  name: $DBOPS_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  sgCluster: $CLUSTER_NAME
  op: minorVersionUpgrade
  minorVersionUpgrade:
    postgresVersion: "$E2E_MINOR_TARGET_POSTGRES_VERSION"
EOF

  kubectl get sgdbops -n "$CLUSTER_NAMESPACE" "$DBOPS_NAME" -o json \
    | jq "$(cat << EOF
      . as \$dbops | {} | {
        metadata: {
          name: "$DBOPS_NAME.1-test",
          namespace: "$CLUSTER_NAMESPACE"
        },
        apiVersion: "v1",
        kind: "Event",
        count: 1,
        eventTime: null,
        firstTimestamp: "2021-06-15T15:29:39Z",
        lastTimestamp: "2021-06-21T17:20:46Z",
        type: "Normal",
        message: "StackGres DbOps $CLUSTER_NAMESPACE.$DBOPS_NAME started",
        reason: "DbOpsStarted",
        reportingComponent: "",
        reportingInstance: "",
        involvedObject: {
          apiVersion: \$dbops.apiVersion,
          kind: \$dbops.kind,
          namespace: \$dbops.metadata.namespace,
          name: \$dbops.metadata.name,
          resourceVersion: \$dbops.metadata.resourceVersion,
          uid: \$dbops.metadata.uid
        },
        source: {
          component: "stackgres-operator"
        }
      }
EOF
    )" | kubectl create -f -

  wait_until eval 'kubectl get pod -n "$CLUSTER_NAMESPACE" -l stackgres.io/db-ops=true -o name | wc -l | grep -q "^1$"'
}

e2e_test() {
  run_test "Check that dbops events can be accessed through the API" check_dbops_events

  run_test "User with wrong credentials should not be authenticated" test_authentication_api_error
}

check_dbops_events() {
  if run_curl -r "stackgres/namespaces/$CLUSTER_NAMESPACE/sgdbops/$DBOPS_NAME/events" \
    | jq -r "any(.metadata.namespace == \"$CLUSTER_NAMESPACE\" and .involvedObject.kind == \"SGDbOps\" and .involvedObject.name == \"$DBOPS_NAME\")" \
    | grep -q "^true$"
  then
    success "SGDbOps events for $DBOPS_NAME included in json response"
  else
    fail "SGDbOps events for $DBOPS_NAME not included in json response"
  fi

  if run_curl -r "stackgres/namespaces/$CLUSTER_NAMESPACE/sgdbops/$DBOPS_NAME/events" \
    | jq -r "any(.metadata.namespace == \"$CLUSTER_NAMESPACE\" and .involvedObject.kind == \"Job\")" \
    | grep -q "^true$"
  then
    success "Job events included in json response"
  else
    fail "Job events not included in json response"
  fi

  if run_curl -r "stackgres/namespaces/$CLUSTER_NAMESPACE/sgdbops/$DBOPS_NAME/events" \
    | jq -r "any(.metadata.namespace == \"$CLUSTER_NAMESPACE\" and .involvedObject.kind == \"Pod\")" \
    | grep -q "^true$"
  then
    success "Pods events included in json response"
  else
    fail "Pods events not included in json response"
  fi
}

test_authentication_api_error() {
  local HTTP_STATUS

  HTTP_STATUS="$(run_curl -j "$FAKE_JWT" -r "stackgres/sgdbops/events" -n "$CLUSTER_NAMESPACE" -e "-LI -o /dev/null -w %{http_code}")"

  check_authentication_error
}

check_authentication_error() {
  if [ "$HTTP_STATUS" = "401" ]
  then
    success "Request returned expected authentication error"
  else
    fail "Request returned unexpected response status $HTTP_STATUS instead of the expected authentication error."
  fi
}

